package com.angus.modules.content.repository;


import cn.hutool.core.text.CharSequenceUtil;
import com.angus.api.content.domain.OrderCommission;
import com.angus.modules.content.mapper.OrderCommissionMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author lineCode
 * @date 2025-08-17
 * @description 订单佣金仓库
 */
@Repository
public class OrderCommissionRepository extends ServiceImpl<OrderCommissionMapper, OrderCommission> {
    /**
     * 订单佣金列表
     * @param orderNo 订单号
     * @param courseCode 课程码
     * @param offset 偏移量
     * @param limit 限制条数
     * @return 订单佣金列表
     */
    public List<OrderCommission> getOrderCommissionList(String orderNo, String courseCode, String courseName, Long userId, int offset, int limit) {
        return baseMapper.getOrderCommissionList(orderNo, courseCode, courseName, userId, offset, limit);
    }

    /**
     * 订单总金额
     * @param orderNo 订单号
     * @param courseCode 课程码
     * @return 订单总金额列表
     */
    public Map<String,Object> getOrderCommissionSummary(String orderNo, String courseCode) {
        return baseMapper.sumCommission(orderNo, courseCode);
    }

    /**
     * 订单佣金总数
     * @param orderNo 订单号
     * @param courseCode 课程码
     * @return 订单佣金总数
     */
    public int countOrderCommission(String orderNo, String courseCode, String courseName) {
        LambdaQueryWrapper<OrderCommission> queryWrapper = new LambdaQueryWrapper<>();
        if (CharSequenceUtil.isNotBlank(orderNo)) {
            queryWrapper.eq(OrderCommission::getOrderNo, orderNo);
        }
        if (CharSequenceUtil.isNotBlank(courseCode)) {
            queryWrapper.eq(OrderCommission::getCourseCode, courseCode);
        }
        if (CharSequenceUtil.isNotBlank(courseName)) {
            queryWrapper.like(OrderCommission::getCourseName, courseName);
        }
        queryWrapper.eq(OrderCommission::getDelFlag, 0);
        return Math.toIntExact(baseMapper.selectCount(queryWrapper));
    }
}
